package cn.kgc.auth.controller;

import cn.kgc.auth.service.UserService;
import cn.kgc.dto.Dto;
import cn.kgc.pojo.User;
import cn.kgc.vo.UserToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.mobile.device.Device;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 文档注释 生成 帮助文件
 * RestController  == Controller  +  ResponseBody
 *          返回的是JSON
 */
@RestController
@RequestMapping("/api/")
public class UserController {
    @Resource
    private UserService userService;
    /**
     * 判断用户是否存在
     * @param name
     * @return
     */
    @GetMapping("ckusr")
    @ApiOperation("判断用户是否存在")
    public Dto ckusr(String name){
        return userService.checkUserCode(name);
    }
    /**
     * 邮箱注册
     * @param user
     * @return
     */
    @PostMapping("doregister")
    @ApiOperation("邮箱注册")
    public Dto doregister(@RequestBody User user){
        return userService.registerByEmail(user);
    }

    /**
     * 手机注册
     * @param user
     * @return
     */
    @PostMapping("registerbyphone")
    @ApiOperation("手机注册")
    public Dto registerbyphone(@RequestBody User user){
        return userService.registerByPhone(user);
    }

    /**
     *用户激活
     * @param user
     * @param code
     * @return
     */
    @PutMapping("activate")
    @ApiOperation("用户激活")
    public Dto activate(String user,String code){
        return userService.userActivate(user,code);
    }

    /**
     * 登录
     * @param name
     * @param password
     * @return
     */
    @PostMapping("dologin")
    public Dto<UserToken> dologin(String name, String password, Device device){
        String logo="pc";
        if (device.isMobile()){
            logo="mobile";
            return userService.login(name,password,logo);
        }
        return userService.login(name,password,logo);
    }

    /**
     * 注销
     * @param request
     * @return
     */
    @GetMapping(value = "loginout",headers = "token")
    public Dto loginout(HttpServletRequest request){
        String token=request.getHeader("token");
        return userService.loginOut(token);
    }

    /**
     * token维护
     * @param request
     * @return
     */
    @PostMapping(value = "retoken",headers = "token")
    public Dto retoken(HttpServletRequest request){
        String token=request.getHeader("token");
        return userService.reToken(token);
    }
}
